package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.ExamResponse;
import com.han.bom.Question;
import com.han.bom.Student;
import com.han.dao.AnswerDAO;
import com.han.dao.ExamResponseDAO;
import com.han.dao.QuestionDAO;

/**
 * Servlet implementation class SubmitExamServlet
 */
@WebServlet("/Student/SubmitExamServlet")
public class SubmitExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private Logger logger = Logger.getLogger(getClass().getName()); 
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SubmitExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered SubmitExamServlet POST");
		HttpSession session = request.getSession();
		
		Student student = null;
		String examID = null,type=null;
		QuestionDAO questiondao = new QuestionDAO();
		AnswerDAO answerdao = new AnswerDAO();
		ExamResponseDAO examresponsedao = new ExamResponseDAO();
		session.setAttribute("time",null);
		
		if(session.getAttribute("student")!=null){
			student = (Student)session.getAttribute("student");
			
		}
		else{
			request.setAttribute("msg", "Error!");
			request.setAttribute("type", "fail");
			
			RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
			rd.forward(request,response);
		}
		
		if(request.getParameter("examID")!=null){
			examID = request.getParameter("examID");
		}
		
		if(request.getParameter("type")!=null){
			type = request.getParameter("type");
		}
		
		if(student!=null){
			
			try {
				
				ArrayList<Question> questions = questiondao.retrieveQuestionsbyExam(examID);
				
				for(Question question:questions){
					ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
					question.setChoices(choices);
					question.setAnswer(choices.get(0));
				}
				
				ExamResponse examresponse = student.getExamAnswers().get(0);
				
				for(Question question: questions){
					if(request.getParameter(question.getQuestionID())!=null){
						examresponse.addAnswer(question, request.getParameter(question.getQuestionID()));
					}
					else{
						examresponse.addAnswer(question, "");
					}
				}
				
				examresponsedao.submitResponses(examresponse);
				
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
			student.getExamAnswers().clear();
			response.sendRedirect("PreTakeExamServlet?type="+type);
		}
	}
}
